const XLSX = require("xlsx");
const csv = require("fast-csv");
const fs = require("fs");
const { nanoid } = require("nanoid");

const data = XLSX.readFile("./data.xlsx");
const result = [];

const zjData = XLSX.utils.sheet_to_json(data.Sheets["专讲"]);

zjData.forEach((item) => {
  result.push({
    id: nanoid(),
    name: item["姓名"],
    company: item["分公司"],
    number: item["P13账号"],
    group: "专讲",
    status: 0,
  });
});

const zxData = XLSX.utils.sheet_to_json(data.Sheets["组训"]);

zxData.forEach((item) => {
  result.push({
    id: nanoid(),
    name: item["姓名"],
    company: item["分公司"],
    number: item["P13账号"],
    group: "组训",
    status: 0,
  });
});

const xlsData = XLSX.utils.sheet_to_json(data.Sheets["训练师"]);

xlsData.forEach((item) => {
  result.push({
    id: nanoid(),
    name: item["姓名"],
    company: item["分公司"],
    number: item["从业代码"],
    group: "训练师",
    status: 0,
  });
});

const writeStream = fs.createWriteStream("data.csv");
csv.write(result, { headers: true }).pipe(writeStream);
writeStream.on("finish", function () {
  console.log("CSV文件已成功写入");
});

const hzData = XLSX.utils.sheet_to_json(data.Sheets["汇总"]);

const hzList = hzData.map((item) => {
  console.debug(item);
  return {
    company: item["分公司"],
    total: item["合计"],
  };
});

console.debug(hzList);
